//
//  XQRequestManager.m
//  iLovewCooking
//
//  Created by apple on 2018/8/10.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "XQRequestManager.h"
#import <AFNetworking.h>
#import <AdSupport/AdSupport.h>

@interface XQRequestManager ()
@property(nonatomic, strong) AFHTTPSessionManager *manager;
@property(nonatomic, strong) NSMutableDictionary *baseData;
@end

@implementation XQRequestManager

static NSString *HOME_URL = @"https://api.hyhuo.com/home/list";

- (NSMutableDictionary *)baseData{
    if (!_baseData) {
        _baseData = [NSMutableDictionary dictionary];
        _baseData[@"channel"] = @"appstore";
        _baseData[@"device_name"] = @"iPhone";
        _baseData[@"lang"] = @"zh-Hans-CN";
        _baseData[@"source"] = @"iOS";
        _baseData[@"version_code"] = @"3.3.1";
        _baseData[@"system_version"] = [[UIDevice currentDevice] systemVersion];
        _baseData[@"device_code"] = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    }
    NSString *time = [NSString stringWithFormat:@"%ld",(long)[[NSDate date] timeIntervalSince1970]];
    NSString *nonce = [NSString stringWithFormat:@"%@%.5d",time,arc4random()%10000];
    NSString *uuid = [time md5];
    NSString *token = [[NSString stringWithFormat:@"%@%@%@%@",uuid,@"61de9b1b0e239f06434093c2d1f6c3ea",time,nonce] md5];
    _baseData[@"timestamp"] = time;
    _baseData[@"nonce"] = nonce;
    _baseData[@"token"] = token;
    _baseData[@"uuid"] = uuid;
    return _baseData;
}

static dispatch_once_t onceToken;
static XQRequestManager *istance = nil;
+ (instancetype)shareInstance {
    dispatch_once(&onceToken, ^{
        istance = [[XQRequestManager alloc] init];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
        securityPolicy.allowInvalidCertificates = YES;
        securityPolicy.validatesDomainName = NO;
        manager.securityPolicy = securityPolicy;
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.requestSerializer.timeoutInterval = 6;
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript",@"text/plain",@"image/gif", nil];
        istance.manager = manager;
    });
    return istance;
}

/** 封装POST */
- (void)post:(NSString *)url parameters:(NSMutableDictionary *)parameter success:(success)success failure:(failure)failure{
    [self post:url parameters:parameter progress:nil success:success failure:failure];
}

- (void)post:(NSString *)url parameters:(NSMutableDictionary *)parameter progress:(progress)progress success:(success)success failure:(failure)failure {
    [self.manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

// 主页数据
- (void)GetHomeData:(success)success failure:(failure)failure{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:self.baseData];
    param[@"display_status"] = @0;
    param[@"gesture"] = @"default";
    param[@"is_new"] = @1;
    param[@"last_id"] = @0;
    param[@"sort"] = @"recommend";
    param[@"uid"] = @0;
    param[@"page"] = @1;
    [self post:HOME_URL parameters:param success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

@end
